﻿using System;
using System.Data;
using System.Linq;

using WinnerCMS.Common;
using WinnerCMS.Model;
namespace WinnerCMS.Data
{
    /// <summary>
    /// 采集历史
    /// </summary>
    public class GatherHistoryDal
    {
        /// <summary>
        /// 记录Url
        /// </summary>
        /// <param name="url"></param>
        public void LogUrl(string url)
        {
            using (Entities db = new Entities())
            {
                string md5 = url.Md5();
                bool exists = db.GatherHistores.Any(x => x.Md5 == md5);
                if (exists == false)
                {
                    GatherHistory info = new GatherHistory
                    {
                        Url = url
                        ,
                        Md5 = md5
                    };

                    db.GatherHistores.Add(info);
                    db.SaveChangesAsync().Wait();
                }
            }
        }
        /// <summary>
        /// URL 是否存在
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public bool Exists(string url)
        {
            using (Entities db = new Entities())
            {
                string md5 = url.Md5();
                var query = from x in db.GatherHistores where x.Md5 == md5 select x;

                return query.Any();
            }
        }
    }
}